package org.rendao.auth.assemble;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.rendao.auth.data.UserDetailDO;
import org.rendao.auth.data.UserRoleDO;
import org.rendao.auth.model.UserDetail;
import org.rendao.auth.model.UserRole;
import org.rendao.utils.AssembleUtils;

public class UserDetailAssembler {

	public static UserDetail doToModel(UserDetailDO detailDO) {
		UserDetail detail = new UserDetail();
		detail.setId(detailDO.getId());
		detail.setUsername(detailDO.getUsername());
		detail.setDisplayName(detailDO.getDisplayName());
		detail.setIsDeleted(detailDO.getIsDeleted());
		detail.setPassword(detailDO.getPassword());
		
		List<UserRole> roles = new ArrayList<UserRole>();
		for(UserRoleDO roleDO : detailDO.getRoles()) {
			UserRole role = AssembleUtils.shadowCopy(roleDO, UserRole.class);
			roles.add(role);
		}
		detail.setRoles(roles);
		return detail;
	}
	
	public static UserDetailDO modelToDo(UserDetail detail) {
		UserDetailDO detailDO = new UserDetailDO();
		detailDO.setId(detail.getId());
		detailDO.setUsername(detail.getUsername());
		detailDO.setDisplayName(detail.getDisplayName());
		detailDO.setIsDeleted(detail.getIsDeleted());
		detailDO.setPassword(detail.getPassword());
		
		Set<UserRoleDO> roles = new HashSet<UserRoleDO>();
		for(UserRole role : detail.getRoles()) {
			UserRoleDO roleDO = AssembleUtils.shadowCopy(role, UserRoleDO.class);
			roles.add(roleDO);
		}
		detailDO.setRoles(roles);
		return detailDO;
	}
}
